package me.hgj.jetpackmvvm.network

import android.net.ParseException
import com.google.gson.JsonParseException
import com.google.gson.stream.MalformedJsonException
import org.apache.http.conn.ConnectTimeoutException
import org.json.JSONException
import retrofit2.HttpException
import java.net.ConnectException

/**
 * 作者　: hegaojian
 * 时间　: 2019/12/17
 * 描述　: 根据异常返回相关的错误信息工具类
 */
object ExceptionHandle {

    fun handleException(e: Throwable?): AppException = when (e) {
        is HttpException -> {
            AppException(Error.NETWORK_ERROR, e)

        }
        is JsonParseException, is JSONException, is ParseException, is MalformedJsonException -> {
            AppException(Error.PARSE_ERROR, e)

        }
        is ConnectException -> {
            AppException(Error.NETWORK_ERROR, e)

        }
        is javax.net.ssl.SSLException -> {
            AppException(Error.SSL_ERROR, e)

        }
        is ConnectTimeoutException -> {
            AppException(Error.TIMEOUT_ERROR, e)

        }
        is java.net.SocketTimeoutException -> {
            AppException(Error.TIMEOUT_ERROR, e)

        }
        is java.net.UnknownHostException -> {
            AppException(Error.TIMEOUT_ERROR, e)

        }
        is AppException -> e

        else -> {
            AppException(Error.UNKNOWN, e)

        }
    }

}